package com.zhengdongzulin.zuke.app.main.mine.collection

import androidx.lifecycle.rxLifeScope
import com.lxj.androidktx.livedata.StateLiveData
import com.zhengdongzulin.zuke.app.Api
import com.zhengdongzulin.zuke.app.data.CollectionHouseType
import com.zhengdongzulin.zuke.app.data.FindHouse
import com.zhengdongzulin.zuke.base.BaseViewModel
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toResponse

class CollectionFootprintVm : BaseViewModel() {

    var access_list: StateLiveData<MutableList<FindHouse>> = StateLiveData()

    fun access_list() {
        rxLifeScope.launch(
            {
                var r = RxHttp.postJson(Api.ACCESS_LIST)
                    .toResponse<MutableList<FindHouse>>()
                    .await()
                collection_list.postValueAndSuccess(r)
            }, {
                assignment(it, access_list)
            }
        )
    }

    var access_list_room_type: StateLiveData<MutableList<CollectionHouseType>> = StateLiveData()

    fun access_list_room_type() {
        rxLifeScope.launch(
            {
                var r = RxHttp.postJson(Api.ACCESS_LIST_ROOM_TYPE)
                    .toResponse<MutableList<CollectionHouseType>>()
                    .await()
                collection_list_room_type.postValueAndSuccess(r)
            }, {
                assignment(it, access_list_room_type)
            }
        )
    }

    var collection_list: StateLiveData<MutableList<FindHouse>> = StateLiveData()

    fun collection_list() {
        rxLifeScope.launch(
            {
                var r = RxHttp.postJson(Api.COLLECTION_LIST)
                    .toResponse<MutableList<FindHouse>>()
                    .await()
                collection_list.postValueAndSuccess(r)
            }, {
                assignment(it, collection_list)
            }
        )
    }

    var collection_list_room_type: StateLiveData<MutableList<CollectionHouseType>> = StateLiveData()

    fun collection_list_room_type() {
        rxLifeScope.launch(
            {
                var r = RxHttp.postJson(Api.COLLECTION_LIST_ROOM_TYPE)
                    .toResponse<MutableList<CollectionHouseType>>()
                    .await()
                collection_list_room_type.postValueAndSuccess(r)
            }, {
                assignment(it, collection_list_room_type)
            }
        )
    }

    var reservation_list: StateLiveData<MutableList<FindHouse>> = StateLiveData()

    fun reservation_list() {
        rxLifeScope.launch(
            {
                var r = RxHttp.postJson(Api.RESERVATION_LIST)
                    .toResponse<MutableList<FindHouse>>()
                    .await()
                reservation_list.postValueAndSuccess(r)
            }, {
                assignment(it, reservation_list)
            }
        )
    }

    var reservation_list_room_type: StateLiveData<MutableList<CollectionHouseType>> =
        StateLiveData()

    fun reservation_list_room_type() {
        rxLifeScope.launch(
            {
                var r = RxHttp.postJson(Api.RESERVATION_LIST_ROOM_TYPE)
                    .toResponse<MutableList<CollectionHouseType>>()
                    .await()
                reservation_list_room_type.postValueAndSuccess(r)
            }, {
                assignment(it, reservation_list_room_type)
            }
        )
    }

    var book_list: StateLiveData<MutableList<FindHouse>> = StateLiveData()

    fun book_list() {
        rxLifeScope.launch(
            {
                var r = RxHttp.postJson(Api.BOOK_LIST)
                    .toResponse<MutableList<FindHouse>>()
                    .await()
                reservation_list.postValueAndSuccess(r)
            }, {
                assignment(it, book_list)
            }
        )
    }

    var book_list_room_type: StateLiveData<MutableList<CollectionHouseType>> = StateLiveData()

    fun book_list_room_type() {
        rxLifeScope.launch(
            {
                var r = RxHttp.postJson(Api.BOOK_LIST_ROOM_TYPE)
                    .toResponse<MutableList<CollectionHouseType>>()
                    .await()
                reservation_list_room_type.postValueAndSuccess(r)
            }, {
                assignment(it, book_list_room_type)
            }
        )
    }

    var reservation_cancel: StateLiveData<String> = StateLiveData()

    fun reservation_cancel(id: String) {
        rxLifeScope.launch(
            {
                var r = RxHttp.postJson(Api.RESERVATION_CANCEL)
                    .add("id", id)
                    .toResponse<String>()
                    .await()
                reservation_cancel.postValueAndSuccess(r)
            }, {
                assignment(it, reservation_cancel)
            }
        )
    }

    var check_sign_online: StateLiveData<String> = StateLiveData()

    fun check_sign_online(key: String, id: String?) {
        rxLifeScope.launch(
            {
                var r = RxHttp.postJson(Api.CHECK_SIGN_ONLINE)
                    .add(key, id)
                    .toResponse<String>()
                    .await()
                check_sign_online.postValueAndSuccess(r)
            }, {
                assignment(it, check_sign_online)
            }
        )
    }

}